package com.pigotech.ptwo.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

/**
 * Created by ghb on 2018/3/16.
 */

public class UtilFile {
    public static boolean saveFile(InputStream inputStream, String path){
        if(inputStream == null){
            return false;
        }
        File file = new File(path);
        file.mkdirs();
        file = new File(path);
        try {
            if(file.exists()){
                file.delete();
            }
            file.createNewFile();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = inputStream.read(bytes)) != -1) {
                byteArrayOutputStream.write(bytes, 0, length);
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file,
                    false);
            fileOutputStream.write(byteArrayOutputStream.toByteArray());
            fileOutputStream.flush();
            fileOutputStream.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean createFolder(String path) {
        File file = new File(path);
        return file.mkdirs();
    }

    /**
     * 将目标file解析成字节数组
     * @param file
     * @return
     */
    public static byte[] convertFileToByte(String file){
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        byte[] result = new byte[]{};
        try {
            FileInputStream inputStream = new FileInputStream(file);
            int length = -1;
            while((length = inputStream.read(buffer)) != -1){
                arrayOutputStream.write(buffer, 0, length);
            }
            result = arrayOutputStream.toByteArray();

            inputStream.close();
            arrayOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 将字节数组添加到文件末尾
     * @param bytes
     * @param path
     * @return
     */
    public static boolean andBytesToFile(byte[] bytes, String path) {
        O.o("save to:"+path);
        return andBytesToFile(bytes, 0, bytes.length, path);
    }

    /**
     * 将 len 个字节从指定 byte 数组写入到此文件，并从偏移量 off 处开始。
     * @param bytes 数据。
     * @param off 数据的初始偏移量。
     * @param len 要写入的字节数。
     * @param path 路径
     * @return
     */
    public static boolean andBytesToFile(byte[] bytes, int off, int len, String path) {
        boolean sucess = true;
        RandomAccessFile fos = null;
        try {
            fos = new RandomAccessFile(path,"rwd");
            long fileLength = fos.length();
            fos.seek(fileLength);
            fos.write(bytes, off, len);
            O.o(fos.getFilePointer()+"--------");
        } catch (FileNotFoundException e) {
            sucess  = false;
            e.printStackTrace();
        } catch (IOException e) {
            sucess  = false;
            e.printStackTrace();
        }finally {
            try {
                if(fos!=null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sucess;
    }
    /**
     * 返回指定文件末尾特定长度字节数据
     * @param len 要读出的字节数
     * @param path 文件路径
     * @return
     */
    public static byte[] atEndReadFully(int len, String path) {

        long fileLen = fileLength(path);
        int off = 0;

        if(fileLen < len) {
            len = (int) fileLen;
        } else {
            off = (int) (fileLen - len);
        }
        byte[] bytes = new byte[len];
        readFully(bytes, off, len, path);

        return bytes;
    }

    /**
     * 将特定文件正好 len 个字节从此文件读入 byte 数组，并从当前文件指针开始。
     * @param bytes 读入数据的缓冲区。
     * @param off 数据的初始偏移量。
     * @param len 要读取的字节数。
     * @param path 文件路径
     */
    public static void readFully(byte[] bytes, int off, int len, String path) {
        RandomAccessFile fos = null;
        try {
            fos = new RandomAccessFile(path,"rwd");
            fos.readFully(bytes, off, len);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fos!=null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 特到指定文件的长度
     * @param path
     * @return
     */
    public static long fileLength(String path) {
        RandomAccessFile fos = null;
        try {
            fos = new RandomAccessFile(path,"rwd");
            return fos.length();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return -1;
    }

    public static void deleteFile(String path){
        if(path!=null && !path.equals("")){
            File file = new File(path);
            file.delete();
        }
    }

}
